import { md5, toSuccess, useEntityManager, useRedis } from "@sker/core"
import { KsPage } from "@sker/entities"
import { useData } from "@sker/runtime"
import { z } from "zod"

export default async () => {
    const input = z.object({
        packageName: z.string(),
        activityName: z.string(),
        deviceUuid: z.string(),
        content: z.string(),
        title: z.string(),
        name: z.string(),
    })
    const body = input.parse(useData())
    await useRedis(8, async r => {
        await r.hSet('bugs', body.name, body.content)
    });
    return toSuccess(true)
    return useEntityManager(async m => {
        const hash = md5(body.content)
        const one = await m.findOne(KsPage, { where: { name: body.name } })
        const data = m.create(KsPage, {
            id: one?.id,
            package_name: body.packageName,
            activity_name: body.activityName,
            html: body.content,
            hash: hash,
            device_uuid: body.deviceUuid,
            title: body.title,
            name: body.name
        });
        await m.save(KsPage, data)
        return toSuccess(true)
    })
}
